package com.ldl.origami.origamiService;

import com.ldl.origami.dao.*;
import com.ldl.origami.domain.*;
import com.ldl.origami.util.OrigamiUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能描述:
 * 作者: LDL
 * 创建时间: 2014/8/20 15:19
 */
@Service
public class UserService {

    @Autowired(required = false)
    private NewsMapper newsMapper;

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired(required = false)
    private TutorialMapper tutorialMapper;

    @Autowired(required = false)
    private UserScoreMapper userScoreMapper;

    @Autowired(required = false)
    private TutorialStoreMapper tutorialStoreMapper;

    static Logger logger = LoggerFactory.getLogger(UserService.class);

    /**
     * 根据用户名查询消息
     * @param userName
     * @return
     */
    public List<News> getNewsByUserName(String userName) throws Exception {
        NewsExample example = new NewsExample();
        example.createCriteria().andUserNameEqualTo(userName);
        example.or().andUserNameEqualTo("ALL");
        List<News> newses = null;
        try {
            newses = newsMapper.selectByExample(example);
        } catch (Exception e) {
            logger.debug("查询消息失败");
            throw new Exception();
        }
        if(newses!=null && newses.size()>0){
            return newses;
        }
        return null;
    }


    /**
     * 根据消息ID查询消息
     * @param newsId
     * @return
     */
    public News getNewsById(int newsId){
        News news = newsMapper.selectByPrimaryKey(newsId);
        return news;
    }


    /**
     * 根据newsId更新消息状态
     * @param newsId
     */
    public void updateNewsById(int newsId){
        News news = new News();
        news.setNewsId(newsId);
        news.setState(1);
        newsMapper.updateByPrimaryKeySelective(news);
    }

    /**
     * 根据用户名修改用户积分
     * @param user
     */
    public void updateIntegralByUserName(User user) throws Exception {

        try {
            userMapper.updateByPrimaryKeySelective(user);
        } catch (Exception e) {
            logger.debug("更新用户积分出错: "+ OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }

    }

    /**
     * 根据用户名
     * @param userName
     * @return
     * @throws Exception
     */
    public User getUserByUserName(String userName) throws Exception {
        User user = null;
        try {
            user = userMapper.selectByPrimaryKey(userName);
        } catch (Exception e) {
            logger.debug("获取用户信息出错: " + OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }
        return user;
    }


    /**
     * 更新用户账号
     * @param user,userName
     * @throws Exception
     */
    public void updateUserAccount(User user,String userName) throws Exception {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUserNameEqualTo(userName);

        News news = new News();
        news.setUserName(user.getUserName());
        NewsExample newsExample = new NewsExample();
        newsExample.createCriteria().andUserNameEqualTo(userName);

        Tutorial tutorial = new Tutorial();
        tutorial.setUserName(user.getUserName());
        TutorialExample tutorialExample = new TutorialExample();
        tutorialExample.createCriteria().andUserNameEqualTo(userName);

        UserScore userScore = new UserScore();
        userScore.setUserName(user.getUserName());
        UserScoreExample userScoreExample = new UserScoreExample();
        userScoreExample.createCriteria().andUserNameEqualTo(userName);


        TutorialStore tutorialStore = new TutorialStore();
        tutorialStore.setUsername(user.getUserName());
        TutorialStoreExample tutorialStoreExample = new TutorialStoreExample();
        tutorialStoreExample.createCriteria().andUsernameEqualTo(userName);

        try {
            userMapper.updateByExampleSelective(user,userExample);
            newsMapper.updateByExampleSelective(news,newsExample);
            tutorialMapper.updateByExampleSelective(tutorial,tutorialExample);
            userScoreMapper.updateByExampleSelective(userScore,userScoreExample);
            tutorialStoreMapper.updateByExampleSelective(tutorialStore,tutorialStoreExample);
        } catch (Exception e) {
            logger.debug("更新账号信息出错: " + OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }
    }


    /**
     * 检验旧密码
     * @param user
     * @return
     * @throws Exception
     */
    public List<User> checkOldPasswd(User user) throws Exception {

        UserExample example = new UserExample();
        example.createCriteria().andUserNameEqualTo(user.getUserName()).andUserPasswdEqualTo(user.getUserPasswd());
        List<User> users = null;
        try {
            users = userMapper.selectByExample(example);
        } catch (Exception e) {
            logger.debug("检测原密码出错: " + OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }
        return users;
    }

    /**
     * 更新密码
     * @param user
     */
    public void updateUserPasswd(User user) throws Exception {

        try {
            userMapper.updateByPrimaryKeySelective(user);
        } catch (Exception e) {
            logger.debug("修改密码出错: " + OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }
    }


    /**
     * 更新头像
     *
     * @param user
     */
    public void updateUserHeadImage(User user) throws Exception {

        try {
            userMapper.updateByPrimaryKeySelective(user);
        } catch (Exception e) {
            logger.debug("修改头像出错: " + OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }
    }

    /**
     * 更新个人简介
     *
     * @param user
     */
    public void updateUserProfile(User user) throws Exception {

        try {
            userMapper.updateByPrimaryKeySelective(user);
        } catch (Exception e) {
            logger.debug("修改个人简介出错: " + OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }
    }
}
